package com.taxigang.client;

/*
 * 
 *
 *
 */

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.facebook.FacebookApi;
import com.taxigang.client.facebook.FacebookFactory;
import com.taxigang.client.uibinder.fbpage.Welcome;


public class TaxiGangMain implements EntryPoint {

	public static boolean channelSupported = false;
	
	private final static String TAXIGANGLOCALE = "TAXIGANGLOCALE";
	private final static String CRON = "CRON";

	private TaxigangMode taxigangMode = TaxigangMode.NONE;

	public TaxiGangMain(){
	}

	private final CountryServiceAsync countryService = GWT.create(CountryService.class);


	public void onModuleLoad() {
//		String localeTest = Window.Location.getParameter("locale");
//		System.out.println("locale="+localeTest);
//		Log.info("getLocale="+localeTest);
		Session.getInstance().setLocale(LocaleInfo.getCurrentLocale().getLocaleName(), "unknown");
		
		final String agent = Window.Location.getParameter("agent");
		final String tg = Window.Location.getParameter("tg");
		final String pt = Window.Location.getParameter("pt");
		final String rt = Window.Location.getParameter("rt");
		final String cancel = Window.Location.getParameter("cancel");
		final String id;
		if (pt != null){
			id = pt;
			taxigangMode = TaxigangMode.PARTNER;
		}else
		if (tg != null){
			id = tg;
			taxigangMode = TaxigangMode.TAXIGANG;
		}else
		if (rt != null){
			id = rt;
			taxigangMode = TaxigangMode.ROUTE;
		}else if(cancel != null){
			id=cancel;
			taxigangMode = TaxigangMode.WISHROUTECANCEL;
		}else{
			id = null;
		}
		
		final String mode = Window.Location.getParameter("mode");
		final String cron = Window.Location.getParameter(CRON);
		boolean localMode = GWT.getHostPageBaseURL().contains("http://127.0.0.1:8888/");
		boolean isFB = "FB".equals(Window.Location.getParameter("userAgent"));
		boolean tooNarrow = Window.getClientWidth() < 989;

		if ("GOTO".equals(mode)){
			String href = Window.Location.getHref();
			href = href.replace("mode=GOTO&", "");
			breakOut1(href);
			Window.Location.assign("http://www.taxigang.com"); 
		}

		if ("FB_PAGE".equals(mode)){
			final String pageId = Window.Location.getParameter("page");
			String likedStr = Window.Location.getParameter("liked");
			final boolean  liked = "true".equals(likedStr);
			Log.info("welcome: page:"+pageId);
			Log.info("welcome: liked:"+likedStr);
			//Log.info("welcome: locale "+Window.Location.getParameter("locale"));
			final  Welcome welcome = new Welcome(pageId,liked);
			Command command = new Command(){

				@Override
				public void execute() {
					FacebookApi.initFacebookAPITaxiGang();
					RootPanel.get().add(welcome);

				}
				
			};
			TaxiGangMain.hideLoading();
			FacebookFactory.createFBConnection(null, command);
			return;
		}
		
//		if ("FB_AMBASSADOR".equals(mode)){
//			final String pageId = Window.Location.getParameter("page");
//			String likedStr = Window.Location.getParameter("liked");
//			final boolean  liked = "true".equals(likedStr);
//			Log.info("welcome: page:"+pageId);
//			Log.info("welcome: liked:"+likedStr);
//			Log.info("welcome: locale "+Window.Location.getParameter("locale"));
//			final  Ambassador welcome = new Ambassador(pageId,liked);
//			Command command = new Command(){
//
//				@Override
//				public void execute() {
//					FacebookApi.initFacebookAPITaxiGang();
//					RootPanel.get().add(welcome);
//
//				}
//				
//			};
//			TaxiGangMain.hideLoading();
//			FacebookFactory.createFBConnection(null, command);
//			return;
//		}
		
//		if (!localMode && getLocale())
//			return;
		
		final String locale = Window.Location.getParameter("locale");
		final String country = Window.Location.getParameter("co");
		System.out.println(locale);

		GWT.runAsync(new RunAsyncCallback() {
			public void onFailure(Throwable caught) {
				Window.alert("Code download failed");
			}

			public void onSuccess() {
				new TaxiGangProcessor().onModuleLoad(locale,country,taxigangMode,id,cancel,agent,cron);
			}
		});

	}
//	private boolean getLocale(){
//		String locale = Window.Location.getParameter("locale");
//		Log.info("getLocale="+locale);
//
//		if (locale == null){
//			locale = Cookies.getCookie(TAXIGANGLOCALE);
//			Log.info("locale from cookie="+locale);
//			Log.info("href="+Window.Location.getHref());
//			Log.info("host="+Window.Location.getHost());
//			Log.info("path="+Window.Location.getPath());
//			String prefix = "../../";
////			if (Window.Location.getHref().indexOf("taxiganggwt") >=0){
////				prefix = "../";
////			}
//			String redirect = prefix+"?locale="+locale;
//			Log.info("redirect="+redirect);
//			Window.Location.replace(redirect);
//
//		} else {
//			Cookies.setCookie(TAXIGANGLOCALE,locale);
//		}
//		
//		if (locale == null){
//			String href = Window.Location.getHref();
//			if (href.contains("taxiganggwt")){
//				Window.alert(href);
//			}
//			Window.Location.replace("country");
//			//Window.Location.replace("country");
//			return true;
//		}
//		return false;
//
//	}
	
	public static void hideLoading(){
		//Widget loaderimage = RootPanel.get("loaderimage");
		Widget loaderimage = RootPanel.get("loading");
		loaderimage.setVisible(false);
	}
	
	public native void breakOut1(String href) /*-{
    if ($wnd.top.location != $wnd.location) {
        $wnd.top.location = href;
    }
	}-*/;
}